/**   
 * @Title: DBHelper.java
 * @Package com.willter.weather.dao
 * @Description: TODO(描述文件)
 * 		<p>
 * @author willter
 * @date 2012-9-12
 */
package com.willter.weather.dao;

import java.util.ArrayList;
import java.util.List;

import com.willter.weather.utils.WebAccessTools;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.widget.BaseExpandableListAdapter;

/**
 * @ClassName: DBHelper
 * @Description: 访问数据库工具类：获得省份与城市
 *               <p>
 * @author willter
 * @date 2012-9-12
 */
public class DBHelper extends SQLiteOpenHelper {

	public DBHelper(Context context, String name) {
		this(context, name, null, 2);
	}

	public DBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase paramSQLiteDatabase) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2) {

	}

	/**
	 * @Title: getAllProvinces
	 * @Description: 获取省份，直辖市一维数组
	 *               <p>
	 * @author willter
	 * @date 2012-9-13
	 *       <p>
	 * @return
	 */
	public String[] getAllProvinces() {
		String[] columns = { "name" };

		// 获取SQLite数据库
		SQLiteDatabase db = getReadableDatabase();
		// 查询：获取游标
		Cursor cursor = db.query("provinces", columns, null, null, null, null, null);
		int count = cursor.getCount();
		String[] provinces = new String[count];
		int i = 0;
		while (!cursor.isLast()) {
			cursor.moveToNext();
			provinces[i++] = cursor.getString(0);
		}
		cursor.close();
		db.close();

		return provinces;
	}

	/**
	 * @Title: getAllCityAndCode
	 * @Description: 根据省份，直辖市一维数组，获取对应的城市二维数组集合
	 *               <p>
	 * @author willter
	 * @date 2012-9-13
	 *       <p>
	 * @param provinces
	 * @return
	 */
	public List<String[][]> getAllCityAndCode(String[] provinces) {
		int length = provinces.length;
		String[][] city = new String[length][];
		String[][] code = new String[length][];
		int count = 0;
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = null;
		for (int i = 0; i < length; i++) {
			cursor = db.query("citys", new String[] { "name", "city_num" }, "province_id = ?", new String[] { String.valueOf(i) }, null,
					null, null);
			count = cursor.getCount();
			city[i] = new String[count];
			code[i] = new String[count];
			int k = 0;
			while (!cursor.isLast()) {
				cursor.moveToNext();
				city[i][k] = cursor.getString(0);
				code[i][k] = cursor.getString(1);
				k++;
			}
			cursor.close();
		}
		db.close();

		List<String[][]> result = new ArrayList<String[][]>();
		result.add(city);
		result.add(code);

		return result;
	}

	/**
	 * @Title: getCityCodeByName
	 * @Description: 根据城市名称获取城市码
	 *               <p>
	 * @author willter
	 * @date 2012-9-13
	 *       <p>
	 * @param cityName
	 *            城市名称
	 * @return
	 */
	public String getCityCodeByName(String cityName) {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query("citys", new String[] { "city_num" }, "name = ?", new String[] { cityName }, null, null, null);
		String cityCode = null;
		if (!cursor.isLast()) {
			cursor.moveToNext();
			cityCode = cursor.getString(0);
		}
		cursor.close();
		db.close();

		return cityCode;
	}
}
